﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MessageBoxCountdown : MonoBehaviour
{

    public GameObject m_kMask;
    public UILabel m_kLabel;
    public GameObject m_kOKBtn;
    public GameObject m_kCancelBtn;

    public UILabel titleLable;

    public List<UITweener> tweeners;

    public string strContent;
    public float timer;

    public void OnMaskClick(GameObject obj)
    {
        gameObject.SetActive(false);
    }

    BoxManager.MessageBoxDelegate onClick = null;

    public void Init(string sLabel, bool isConfirmBtnLeft, BoxManager.MessageBoxDelegate onReponse,float timer)
    {
        this.timer = timer;
        this.strContent = sLabel;

        if (strContent.Contains("{0}"))
        {
            m_kLabel.text = string.Format(strContent, (int)timer);
        }
        else
        {
            m_kLabel.text = strContent;
        }

        onClick = onReponse;

        if (!isConfirmBtnLeft)
        {
            Vector3 p = m_kOKBtn.transform.localPosition;
            m_kCancelBtn.transform.localPosition = p;
            p.x = -p.x;
            m_kOKBtn.transform.localPosition = p;
        }

        foreach (var tweener in tweeners)
        {
            tweener.ResetToBeginning();
            tweener.PlayForward();
        }
    }


    public void Init(string sLabel, BoxManager.MessageBoxDelegate onReponse, float timer)
    {
        Init(sLabel, true, onReponse,timer);
    }

    public void Init(string sLabel, bool isConfirmBtnLeft, BoxManager.MessageBoxDelegate onReponse, string title, float timer)
    {
        titleLable.text = title;
        titleLable.gameObject.SetActive(true);
        Init(sLabel, isConfirmBtnLeft, onReponse,timer);
    }

    public void Init(string sLabel, bool isConfirmBtnLeft, BoxManager.MessageBoxDelegate onReponse, string title, bool hasCancleBtn, float timer)
    {
        if (!hasCancleBtn)
        {
            m_kCancelBtn.SetActive(false);
            m_kOKBtn.transform.localPosition = new Vector3(0, -134, 0);
        }
        titleLable.text = title;
        titleLable.gameObject.SetActive(true);
        Init(sLabel, isConfirmBtnLeft, onReponse,timer);
    }

    public void OnConfirmClick(GameObject obj)
    {
        onClick(true);
        Destroy(gameObject);
    }

    public void OnCancelClick(GameObject obj)
    {
        onClick(false);
        Destroy(gameObject);
    }

    void Update()
    {
        if (timer > 0 && strContent.Contains("{0}"))
        {
            timer -= Time.deltaTime;
            if(timer<0f)
            {
                timer = 0f;

                if (onClick!=null)
                {
                    onClick(false);
                }

                Destroy(gameObject);
            }
            m_kLabel.text = string.Format(strContent, (int)timer);
        }
    }
}
